package com.guigui.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigui.common.utils.PageUtils;
import com.guigui.common.utils.Query;
import com.guigui.gulimall.coupon.dao.SeckillPromotionDao;
import com.guigui.gulimall.coupon.entity.SeckillPromotionEntity;
import com.guigui.gulimall.coupon.entity.SeckillSessionEntity;
import com.guigui.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.guigui.gulimall.coupon.service.SeckillPromotionService;
import com.guigui.gulimall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;


@Service("seckillPromotionService")
public class SeckillPromotionServiceImpl extends ServiceImpl<SeckillPromotionDao, SeckillPromotionEntity> implements SeckillPromotionService {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillPromotionEntity> page = this.page(
                new Query<SeckillPromotionEntity>().getPage(params),
                new QueryWrapper<SeckillPromotionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillPromotionEntity> getSeckillSessionLatest3Days() {
        String[] times = latest3Days();
//        List<SeckillPromotionEntity> promotions = this.list(new QueryWrapper<SeckillPromotionEntity>().between("start_time", times[0], times[1]));
//        List<Long> promotionIds = promotions.stream().map(SeckillPromotionEntity::getId).collect(Collectors.toList());
        List<SeckillPromotionEntity> seckillSessionLatest3Days = this.baseMapper.getSeckillSessionLatest3Days(times[0], times[1]);

        List<Long> collect = seckillSessionLatest3Days.stream().map(SeckillPromotionEntity::getId).collect(Collectors.toList());
        List<SeckillSkuRelationEntity> promotion_id = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().in("promotion_id", collect));

        seckillSessionLatest3Days.forEach(promotion -> {
            List<SeckillSessionEntity> seckillSessions = promotion.getSeckillSessions();
            seckillSessions.forEach(session -> {
                Optional<SeckillSkuRelationEntity> relations = promotion_id.stream().filter(relation -> relation.getPromotionSessionId().equals(session.getId())).findFirst();
                session.setSeckillSkuRelation(relations.orElse(null));
            });
        });

        return seckillSessionLatest3Days;
    }

    public String[] latest3Days() {
        LocalDate t1 = LocalDate.now();
        LocalDate t2 = t1.plusDays(2);
        LocalDateTime start = LocalDateTime.of(t1, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(t2, LocalTime.MAX);
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return new String[]{pattern.format(start), pattern.format(end)};
    }

}